package net.maku.newbase;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import net.maku.framework.mybatis.entity.BaseEntity;
import net.maku.framework.mybatis.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

public abstract class NewBaseController<S extends BaseService, E extends BaseEntity> {

    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected S entityService;

    protected void addVerify(E notice) {

    }

    protected void addsVerify(List<E> list) {

    }

    protected void editVerify(E notice) {

    }

    /**
     * 新增
     *
     * @param notice
     * @return
     */
    @Operation(summary = "新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    protected R add(@RequestBody E notice) {
        addVerify(notice);
        return R.ok(entityService.save(notice));
    }

    @Operation(summary = "批量新增")
    @RequestMapping(value = "/adds", method = RequestMethod.POST)
    protected R adds(@RequestBody List<E> list) {
        addsVerify(list);
        return R.ok(entityService.saveBatch(list));
    }

    /**
     * 删除
     *
     * @param idList
     * @return
     */
    @Operation(summary = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public R delete(@RequestBody List<Long> idList) {
        return R.ok(entityService.removeByIds(idList));
    }

    /**
     * 编辑
     *
     * @param notice
     * @return
     */
    @Operation(summary = "编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    protected R edit(@RequestBody E notice) {
        editVerify(notice);
        return R.ok(entityService.updateById(notice));
    }

    /**
     * 主键 查询
     *
     * @param id 主键
     * @return
     */
    @Operation(summary = "主键 查询")
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    protected R findById(String id) {
        return R.ok(entityService.getById(id));
    }
}
